package com.zhuliwei.rabbitmq.direct.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static com.zhuliwei.constants.RabbitConstants.*;


/**
 * Email类型消息队列配置
 *
 * @author 朱力维
 * @time 2022/10/18 13:46
 **/
@Configuration
public class EmailDirectConfig {
    /**
     * 定义消息队列
     *
     * @return
     */
    @Bean
    public Queue directQueue1() {
        // durable:是否持久化，默认false
        // exclusive：是否当前连接专用，默认false，连接关闭后队列即被删除
        // autoDelete：是否自动删除，当生产者或消费者不再使用此队列，自动删除
        return new Queue(EMAIL_QUEUE1, true, false, false);
    }

    @Bean
    public Queue directQueue2() {
        return new Queue(EMAIL_QUEUE2, true, false, false);
    }

    /**
     * 定义交换机
     * 一个交换机可以对多个队列进行操作
     *
     * @return
     */
    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange(EMAIL_EXCHANGE_NAME);
    }

    /**
     * 绑定主题模式（将队列和交换机关联起来）
     *
     * @return routingKey:绑定的名称
     */
    @Bean
    public Binding bindingDirect() {
        return BindingBuilder.bind(directQueue1()).to(directExchange()).with(EMAIL_AUTHCODE_KEY);
    }

    @Bean
    public Binding bindingDirect2() {
        return BindingBuilder.bind(directQueue2()).to(directExchange()).with(EMAIL_MESSAGE_KEY);
    }

}
